// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RegMatch.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   Summary description for RegMatch.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace DbDiff.Common.Model
{
    using System.Collections;
    using System.Linq;
    using System.Text.RegularExpressions;

    /// <summary>
    /// The RegMatch clss
    /// </summary>
    public class RegMatch
    {
        /// <summary>
        /// The comment.
        /// </summary>
        private readonly ArrayList comment = new ArrayList();

        /// <summary>
        /// Initializes a new instance of the <see cref="RegMatch"/> class.
        /// </summary>
        public RegMatch()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RegMatch"/> class.
        /// </summary>
        /// <param name="exs">
        /// The exexuteable objects text
        /// </param>
        public RegMatch(Executes[] exs)
        {
            this.Init(exs);
        }

        /// <summary>
        /// The executes.
        /// </summary>
        public enum Executes
        {
            /// <summary>
            /// The comment 1.
            /// </summary>
            Comment1,

            /// <summary>
            /// The white space.
            /// </summary>
            WhiteSpace,

            /// <summary>
            /// The comment 2.
            /// </summary>
            Comment2,

            /// <summary>
            /// The email.
            /// </summary>
            Email
        }

        /// <summary>
        /// The add executable text
        /// </summary>
        /// <param name="ex">
        /// The exexuteable objects text
        /// </param>
        public void Add(Executes ex)
        {
            switch (ex)
            {
                case Executes.Comment1:
                    this.comment.Add(new Regex("--.*", RegexOptions.Multiline | RegexOptions.Compiled));
                    break;
                case Executes.WhiteSpace:
                    this.comment.Add(new Regex("[ \t\r\n]", RegexOptions.Multiline | RegexOptions.Compiled));
                    break;
                case Executes.Comment2:
                    this.comment.Add(new Regex(@"/\*.*?\*/", RegexOptions.Multiline | RegexOptions.Compiled));
                    break;
                case Executes.Email:
                    this.comment.Add(new Regex(@"^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$", RegexOptions.Multiline | RegexOptions.Compiled));
                    break;
            }
        }

        /// <summary>
        /// The init the execs
        /// </summary>
        /// <param name="exs">
        /// The exexuteable objects text
        /// </param>
        public void Init(Executes[] exs)
        {
            if (this.comment.Count != 0)
            {
                this.comment.Clear();
            }

            foreach (Executes ex in exs)
            {
                this.Add(ex);
            }
        }

        /// <summary>
        /// The replace.
        /// </summary>
        /// <param name="s">
        /// The repleacable string
        /// </param>
        /// <returns>
        /// The replaced string
        /// </returns>
        public string Replace(string s)
        {
            return this.comment.Cast<Regex>().Aggregate(s, (current, re) => re.Replace(current, string.Empty));
        }
    }
}